<div id="orderFormAcceptDiv"></div>
<script type="text/html" id="orderFormAcceptContent">
  <form lay-filter="orderFormAccept" id="orderFormAccept" class="layui-form model-form open-form " style="padding: 5px 30px 0 30px;height: calc(100% - 5px);overflow: hidden;">
    <div class="open-model">
      <div style="height: calc(100% - 100px); padding-top: 20px;overflow: auto;margin-bottom: 20px" class="open-auto" ">
      <div class="layui-form-item layui-row">
        <input name="id" type="hidden" />
        <input name="orderId" type="hidden" />
        <input name="cusProductId" type="hidden" />
        <input name="customerId" type="hidden" />
        <div class="layui-inline layui-col-md3">
          <label class="layui-form-label">客户名称</label>
          <div class="layui-input-inline">
            <input name="customerName" disabled class="layui-input" type="text" />
          </div>
        </div>
        <div class="layui-inline layui-col-md3">
          <label class="layui-form-label">客户联系人</label>
          <div class="layui-input-inline">
            <input name="customerContactsName" disabled class="layui-input" type="text" />
          </div>
        </div>
        <div class="layui-inline layui-col-md3">
          <label class="layui-form-label">联系人电话</label>
          <div class="layui-input-inline">
            <input name="customerContactsPhone" disabled class="layui-input" type="text" />
          </div>
        </div>
        <div class="layui-inline layui-col-md3">
          <label class="layui-form-label">交货时间</label>
          <div class="layui-input-inline">
            <input name="deliveryTime" disabled class="layui-input" type="text" />
          </div>
        </div>
        <!-- <div class="layui-inline layui-col-md3">
          <label class="layui-form-label">所属渠道</label>
          <div class="layui-input-inline">
            <input name="channelName" disabled class="layui-input" type="text" />
          </div>
        </div> -->
        <div class="layui-inline layui-col-md3">
          <label class="layui-form-label">订单类型</label>
          <div class="layui-input-inline">
            <select name="orderType" lay-verify="required" disabled>
              <option value="trial">试产订单</option>
            </select>
          </div>
        </div>
        <div class="layui-inline layui-col-md3">
          <input name="checkerName" type="hidden" />
          <label class="layui-form-label">询价审核人</label>
          <div class="layui-input-inline">
            <input name="checkerId" disabled class="layui-input" type="text" />
          </div>
        </div>
        <div class="layui-inline layui-col-md3">
          <label class="layui-form-label">订单来源</label>
          <div class="layui-input-inline">
            <select name="orderSource" disabled>
              <option value="staff">自主录入/员工录入</option>
              <option value="platform">云端制造/平台指派</option>
              <option value="agent">渠道录入/代理商录入</option>
              <option value="customer">云端智采/客户直接委派</option>
            </select>
          </div>
        </div>
        <div class="layui-inline layui-col-md3">
          <label class="layui-form-label">报价截止时间</label>
          <div class="layui-input-inline">
            <input name="offerEndTime" disabled class="layui-input" type="text" />
          </div>
        </div>

        <table class="layui-table" id="orderFormOneTable" lay-filter="orderFormOneTable"></table>
        <div id="orderFormAcceptTableContent">
          {{# if(d.orderObj.status == 'pending'){ }}
          <button class="layui-btn" type="button" id="addReview">添加评审</button>
          {{# } }}
          <table class="layui-table" id="orderFormAcceptTable" lay-filter="orderFormAcceptTable"></table>
        </div>
      </div>
      <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">关闭</button>
        {{# if(d.orderObj.status == 'pending'){ }}
        <button class="layui-btn" lay-filter="orderFormAcceptSubmit" type="button" lay-submit>提交</button>
        {{# } }}
      </div>
    </div>
  </form>
</script>
<script type="text/html" id="orderFormAcceptTableBar">
  <button class="layui-btn layui-btn-danger layui-btn-xs" type="button" lay-event="del">删除</button>
</script>
<script>
  layui.use(["layer", "form", "table", "admin", "upload", "tree", "laytpl"], async function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var laytpl = layui.laytpl;
    const nowData = { ...parentData };
    let { data } = nowData;
    let orderReviewProcessList = [];
    const orderReviewProcessRes = await admin.reqPro("enterprise/sell/admin/orderReviewProcess/list", {});
    orderReviewProcessList = orderReviewProcessRes.data;

    laytpl(orderFormAcceptContent.innerHTML).render(nowData, function (html) {
      $("#orderFormAcceptDiv").after(html);
    });
    form.val("orderFormAccept", data);
    table.render({
      elem: "#orderFormOneTable",
      data: [nowData.orderObj],
      page: false,
      limit: 999999,
      cols: [
        [
          { type: "numbers", title: "序号" },
          { field: "customerSkuItem", align: "center", title: "客户物料料号" },
          { field: "customerSkuName", align: "center", title: "客户物料名称" },
          { field: "customerSpecCode", align: "center", title: "客户物料规格码" },
          { field: "skuCode", align: "center", title: "内部物料料号" },
          {
            field: "enclosure",
            align: "center",
            title: "附件",
            templet: (d) => {
              return d.enclosure ? `<a href="${d.enclosure}" style="color: blue" >下载附件</a>` : "-";
            }
          },
          { field: "proUnitName", align: "center", title: "单位", width: 60 },
          { field: "quantity", align: "center", title: "数量" },
          { field: "remark", align: "center", title: "备注说明" },
          { field: "statusDesc", align: "center", title: "状态" },
          {
            field: "status",
            align: "center",
            title: "是否受理",
            templet: admin.setTableSelect(
              "status",
              [
                { value: "accepted", label: "受理" },
                {
                  value: "refuse",
                  label: "不受理"
                }
              ],
              false,
              "statusSelect"
            ),
            hide: nowData.orderObj.status != "pending"
          }
        ]
      ],
      done: function () {
        $(".layui-table-body").css("overflow", "visible");
        $(".layui-table-box").css("overflow", "visible");
        $(".layui-table-view").css("overflow", "visible");
        admin.tableSelectChange("orderFormOneTable", "statusSelect", (index, value) => {
          console.log(123, value);
          if (value == "accepted") {
            $("#orderFormAcceptTableContent").css("visibility", "visible");
          } else {
            $("#orderFormAcceptTableContent").css("visibility", "hidden");
          }
        });
      }
    });
    console.log("nowData.orderObj", nowData.orderObj);
    console.log(nowData.orderObj.orderProReviewList);

    if (nowData.orderObj.status != "pending") {
      $("#orderFormAcceptTableContent").css("visibility", "visible");
    } else {
      $("#orderFormAcceptTableContent").css("visibility", "hidden");
    }
    table.render({
      elem: "#orderFormAcceptTable",
      data: [...nowData.orderObj.orderProReviewList],
      page: false,
      limit: 999999,
      height: 250,
      cols:
        nowData.orderObj.status == "pending"
          ? [
              [
                { type: "numbers", title: "序号" },
                {
                  field: "reviewProcessId",
                  align: "center",
                  title: "评审环节",
                  templet: (d) => {
                    let options = [];
                    if (orderReviewProcessList.length > 0) {
                      options = orderReviewProcessList.map((v) => {
                        return {
                          label: v.name,
                          value: v.id
                        };
                      });
                    }
                    return admin.setTableSelect("reviewProcessId", options, false, "reviewProcessId")(d);
                  }
                },
                {
                  field: "reviewPostId",
                  align: "center",
                  title: "评审岗位",
                  templet: (d) => {
                    if (d._reviewPostIdOption) {
                      return admin.setTableSelect("reviewPostId", d._reviewPostIdOption, false, "reviewPostId")(d);
                    } else {
                      return admin.setTableSelect("reviewPostId", [], false, "reviewPostId")(d);
                    }
                  }
                },
                {
                  field: "reviewerId",
                  align: "center",
                  title: "评审人员",
                  templet: (d) => {
                    if (d._reviewerIdOption) {
                      return admin.setTableSelect("reviewerId", d._reviewerIdOption, false, "reviewerId")(d);
                    } else {
                      return admin.setTableSelect("reviewerId", [], false, "reviewerId")(d);
                    }
                  }
                },
                {
                  field: "reviewEndTime",
                  align: "center",
                  title: "截至时间",
                  templet: (d) => {
                    return `<input type="text" class="layui-input " placeholder="交货时间" id="inputTime${d.LAY_TABLE_INDEX}"
                                value="${d.reviewEndTime ? d.reviewEndTime : ""}"  name="deliveryTime" >`;
                  }
                },
                { field: "remark", align: "center", title: "评审备注", templet: admin.setTableInput("remark") },
                { align: "center", toolbar: "#orderFormAcceptTableBar", title: "操作", fixed: "right" }
              ]
            ]
          : [
              [
                { field: "reviewProcessName", align: "center", title: "评审环节" },
                { field: "reviewPostName", align: "center", title: "评审岗位" },
                { field: "reviewerName", align: "center", title: "评审人员" },
                { field: "reviewEndTime", align: "center", title: "截至时间" },
                { field: "remark", align: "center", title: "评审备注" },
                { field: "statusDesc", align: "center", title: "评审状态" },
                { field: "resultDesc", align: "center", title: "评审结果" }
              ]
            ],
      done: function () {
        table.cache["orderFormAcceptTable"].forEach((v, index, list) => {
          laydate.render({
            elem: `#inputTime${index}`,
            type: "datetime",
            done: function (value, date, endDate) {
              list[index].reviewEndTime = value;

              // 在这里处理日期选择后的逻辑
            }
          });
        });

        admin.tableSelectChange("orderFormAcceptTable", "reviewProcessId", (index, value) => {
          table.cache["orderFormAcceptTable"][index].reviewPostId = "";
          if (value) {
            admin.req("enterprise/sell/admin/orderReviewProcessCompany/getRoleData", { processId: value }, function (res) {
              table.cache["orderFormAcceptTable"][index]._reviewPostIdOption = res.data.map((v) => {
                return { value: v.id, label: v.name };
              });
              table.reload("orderFormAcceptTable");
            });
          } else {
            table.cache["orderFormAcceptTable"][index]._reviewPostIdOption = null;
            table.cache["orderFormAcceptTable"][index]._reviewerIdOption = null;
            table.reload("orderFormAcceptTable");
          }
        });
        admin.tableSelectChange("orderFormAcceptTable", "reviewPostId", (index, value) => {
          const list = table.cache["orderFormAcceptTable"][index];
          if (value) {
            admin.req("user/admin/user/list", { postId: value }, function (res1) {
              list._reviewerIdOption = res1.data.map((v) => {
                return { value: v.id, label: v.nickname };
              });
              table.reload("orderFormAcceptTable");
            });
          } else {
            list._reviewerIdOption = null;
            table.reload("orderFormAcceptTable");
          }
        });
        admin.tableSelectChange("orderFormAcceptTable", "reviewerId");
        admin.tableInputChange("orderFormAcceptTable");
      }
    });

    form.on("select(status)", function (d) {
      if (d.value == "accepted") {
        $("#reviewRow").show();
        admin.initDataSelect("enterprise/sell/admin/order/findReview", { type: "trial" }, "reviewProcessId", null, "评审环节");
        admin.initDataSelectTree(true, "user/admin/role/getTreeData", null, "departmentMain", null, "评审部门", "departmentId", function (data) {
          // 获取部门ID
          let departmentId = data.change[0].value;
          //赋值部门id
          $("#departmentId").val(data.change[0].value);
          $("#reviewPostId").empty();
          //部门下的岗位列表
          admin.initDataSelect("user/admin/role/getPostData", { roleId: departmentId }, "reviewPostId", null, "岗位", null, null);
          laydate.render({
            elem: "#reviewEndTime",
            type: "datetime"
          });
        });
      } else {
        $("#reviewRow").hide();
      }
    });
    form.on("submit(orderFormAcceptSubmit)", function (d) {
      let { customerId } = { ...d.field };
      
     
      if (table.cache["orderFormOneTable"][0].status == "pending") {
        layer.msg("请先受理");
        return false;
      }
      if(table.cache["orderFormOneTable"][0].status=='accepted'&&table.cache["orderFormAcceptTable"].length == 0){
        layer.msg("请先添加评审内容");
        return false;
      }
      let formData = {
        id: nowData.orderObj.id,
        orderId: nowData.data.id,
        status: table.cache["orderFormOneTable"][0].status,
        customerId
      };
      formData.orderProReviewList = table.cache["orderFormAcceptTable"].map((v) => {
        v.cusProductId = nowData.orderObj.cusProductId;
        return v;
      });
      admin.req(
        "enterprise/sell/admin/orderPro/update",
        JSON.stringify(formData),
        function (res) {
          if (res.code == 200) {
            admin.req("enterprise/sell/admin/order/detail/" + data.id, {}, function (res) {
              layer.close(nowData.zindex);
              table.reload("orderFormTable", {
                data: res.data.orderProList.map((v) => {
                  v.eventName = "acceptance";
                  return v;
                })
              });
              table.reload("orderTable");
            });
          }
        },
        "PUT",
        true,
        "application/json; charset=utf-8"
      );
    });
    table.on("tool(orderFormAcceptTable)", function (obj) {
      var data = obj.data; // 获取当前行的数据
      var layEvent = obj.event; // 获取事件名（edit 或 del）
      if (layEvent == "del") {
        const index = $(obj.tr[0]).data("index");
        let list = table.cache["orderFormAcceptTable"].filter((v) => {
          return v["LAY_TABLE_INDEX"] != index;
        });
        table.reload("orderFormAcceptTable", {
          data: list
        });
      }
      // return false
    });
    $("#addReview").click(() => {
      let list = table.cache["orderFormAcceptTable"];
      console.log("list", list);
      list.unshift({
        reviewProcessId: "",
        departmentId: "",
        reviewPostId: "",
        reviewEndTime: "",
        remark: ""
      });
      table.reload("orderFormAcceptTable", {
        data: list
      });
      return false;
    });
  });
</script>
<style>
  /*#orderFormAccept .layui-form-label {*/
  /*    width: 100px;*/
  /*}*/

  /*#orderFormAccept .layui-form-item .layui-input-inline {*/
  /*    width: 130px;*/
  /*}*/

  #orderFormAccept .layui-inline {
    margin-bottom: 10px;
    margin-right: 0px;
  }

  #orderFormAccept .layui-table-cell,
  .layui-table-tool-panel li {
    overflow: visible;
  }
</style>
